#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace Osh.Teletext
{
	public class TeletextHtmlWriter : TeletextWriter
	{
		public class TeletextHtmlWriterSettings
		{
			private bool _endRowWithNewLine = true;

			public bool EndRowWithNewLine
			{
				get { return _endRowWithNewLine; }
				set { _endRowWithNewLine = value; }
			}
		}

		// ???, Black, Red, Green, Yellow, Blue, Magenta, Cyan, White
		private static string[] _colorClasses = "cb,cr,cg,cy,cblue,cm,cc,cw".Split(',');
		private static string[] _bgColorClasses = "bgb,bgr,bgg,bgy,bgblue,bgm,bgc,bgw".Split(',');

		private XmlWriter _writer;
		private TeletextHtmlWriterSettings _settings;
		private char[] _chars = new char[1];
		private bool _firstLineWritten;
		private bool _newLineWritten;

		public TeletextHtmlWriter(TextWriter writer)
		{
			_writer = new XmlTextWriter(writer);
			_settings = new TeletextHtmlWriterSettings();
		}

		public override void WriteStartRow(int row)
		{
			if(_firstLineWritten && !_newLineWritten)
				_writer.WriteString("\r\n");

			_newLineWritten = false;

			_writer.WriteStartElement("div");
			_writer.WriteAttributeString("class", "row r" + row.ToString());
		}

		public override void WriteEndRow()
		{
			_writer.WriteFullEndElement();
			if(_settings.EndRowWithNewLine)
			{
				_writer.WriteString("\r\n");
				_newLineWritten = true;
			}
			_firstLineWritten = true;
		}

		public override void WriteStartColumnBlock(int column, RowState state)
		{
			_writer.WriteStartElement("span");
			_writer.WriteAttributeString("class", ToHtmlClass(state, column));
		}

		public override void WriteEndColumnBlock()
		{
			_writer.WriteEndElement();
		}

		public override void WriteChar(char ch)
		{
			if(ch == 0x20)
				_writer.WriteRaw("&nbsp;");
			else
			{
				_chars[0] = ch;
				_writer.WriteChars(_chars, 0, 1);
			}
		}

		public override void Flush()
		{
			_writer.Flush();
		}

		public override void Close()
		{
			_writer.Close();
		}

		public TeletextHtmlWriterSettings Settings
		{
			get { return _settings; }
		}

		private string ToHtmlClass(RowState state, int position)
		{
			StringBuilder sb = new StringBuilder();
			sb.Append("ch c");
			sb.Append(position);
			if(state.Conceal)
			{
				sb.Append(" concealed");
			}
			else
			{
				if(state.MosaicMode && state.IsMosaicChar)
					sb.Append(" g1");

				if(state.Flash)
					sb.Append(" flash");

				if(state.Color != RowState.DefaultForegroundColor)
				{
					sb.Append(' ');
					sb.Append(_colorClasses[state.Color]);
				}
			}

			if(state.BackgroundColor != RowState.DefaultBackgroundColor)
			{
				sb.Append(' ');
				sb.Append(_bgColorClasses[state.BackgroundColor]);
			}

			return sb.ToString();
		}

	}
}
